﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    ui->stackedWidget->setCurrentWidget(ui->pagTeacher);

    ui->tvCourse->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvTeacher->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->tvCourse->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvTeacher->setEditTriggers(QAbstractItemView::NoEditTriggers);


    m_teacherModel = new QStandardItemModel(this);
    m_courseModel = new QStandardItemModel(this);

    ui->tvCourse->setModel(m_courseModel);
    ui->tvTeacher->setModel(m_teacherModel);

    initTeacherModel();
    initCourseModel();
}

void MainWindow::initTeacherModel()
{
    m_teacherModel->setColumnCount(8);
    m_teacherModel->setHeaderData(0,Qt::Horizontal,"编号");
    m_teacherModel->setHeaderData(1,Qt::Horizontal,"姓名");
    m_teacherModel->setHeaderData(2,Qt::Horizontal,"性别");
    m_teacherModel->setHeaderData(3,Qt::Horizontal,"出生时间");
    m_teacherModel->setHeaderData(4,Qt::Horizontal,"职称");
    m_teacherModel->setHeaderData(5,Qt::Horizontal,"学历");
    m_teacherModel->setHeaderData(6,Qt::Horizontal,"工资");
    m_teacherModel->setHeaderData(7,Qt::Horizontal,"婚否");
}

void MainWindow::initCourseModel()
{
    m_courseModel->setColumnCount(8);
    m_courseModel->setHeaderData(0,Qt::Horizontal,"课程编号");
    m_courseModel->setHeaderData(1,Qt::Horizontal,"名称");
    m_courseModel->setHeaderData(2,Qt::Horizontal,"课时");
    m_courseModel->setHeaderData(3,Qt::Horizontal,"教室");
    m_courseModel->setHeaderData(4,Qt::Horizontal,"授课班级");
    m_courseModel->setHeaderData(5,Qt::Horizontal,"学分");
    m_courseModel->setHeaderData(6,Qt::Horizontal,"学期");
    m_courseModel->setHeaderData(7,Qt::Horizontal,"老师名字");
}


void MainWindow::on_actOpenTeacher_triggered()
{


    QFile file(QFileDialog::getOpenFileName(0,"选择文件",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)"));
    if(file.exists()){
        m_teacherModel->clear();
        initTeacherModel();

        file.open(QIODevice::ReadOnly);
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        stream.readLine();
        for(;!stream.atEnd();){
            QStringList datas = stream.readLine().trimmed().split("\t",Qt::SkipEmptyParts);
            m_teacherModel->insertRow(m_teacherModel->rowCount());
            for(int i = 0;i < datas.count();++i){
                m_teacherModel->setData(m_teacherModel->index(m_teacherModel->rowCount() - 1,i),datas.at(i),Qt::DisplayRole);
            }
        }
        ui->stackedWidget->setCurrentWidget(ui->pagTeacher);
        file.close();
    }
}


void MainWindow::on_actOpenCourse_triggered()
{
    QFile file(QFileDialog::getOpenFileName(0,"选择文件",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)"));
    if(file.exists()){
        m_courseModel->clear();
        initCourseModel();
        file.open(QIODevice::ReadOnly);
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        stream.readLine();
        for(;!stream.atEnd();){
            QStringList datas = stream.readLine().trimmed().split("\t",Qt::SkipEmptyParts);
            m_courseModel->insertRow(m_courseModel->rowCount());
            for(int i = 0;i < datas.count();++i){
                m_courseModel->setData(m_courseModel->index(m_courseModel->rowCount() - 1,i),datas.at(i),Qt::DisplayRole);
            }
        }
        ui->stackedWidget->setCurrentWidget(ui->pagCourse);
        file.close();
    }
}


void MainWindow::on_actTeacher_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagTeacher);
}


void MainWindow::on_actCourse_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagCourse);
}


void MainWindow::on_cheEditCourse_stateChanged(int arg1)
{
    Q_UNUSED(arg1);

    if(ui->cheEditCourse->isChecked()){
        ui->tvCourse->setEditTriggers(QAbstractItemView::AllEditTriggers);
    }else{
        ui->tvCourse->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}


void MainWindow::on_cheEditTeacher_stateChanged(int arg1)
{
    Q_UNUSED(arg1);
    if(ui->cheEditTeacher->isChecked()){
        ui->tvTeacher->setEditTriggers(QAbstractItemView::AllEditTriggers);
    }else{
        ui->tvTeacher->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}


void MainWindow::on_btnAppend_clicked()
{
    m_teacherModel->insertRow(m_teacherModel->rowCount());
}


void MainWindow::on_btnAppendCourse_clicked()
{
    m_courseModel->insertRow(m_courseModel->rowCount());
}


void MainWindow::on_btnDeleteCourse_clicked()
{
    if(ui->tvCourse->currentIndex().isValid()){
        m_courseModel->removeRow(ui->tvCourse->currentIndex().row());
    }
}


void MainWindow::on_btnDelete_clicked()
{
    if(ui->tvTeacher->currentIndex().isValid()){
        m_teacherModel->removeRow(ui->tvTeacher->currentIndex().row());
    }
}


void MainWindow::on_btnFindTeacher_clicked()
{
    QString str = ui->editFindTeacher->text();
    if(ui->editFindTeacher->text().isEmpty()){
        for(int i = 0;i < m_teacherModel->rowCount();++i){
            ui->tvTeacher->showRow(i);
        }
    }else{
        for(int i = 0;i < m_teacherModel->rowCount();++i){
            for(int j = 0;j < m_teacherModel->columnCount();++j){
                ui->tvTeacher->hideRow(i);

                if(m_teacherModel->data(m_teacherModel->index(i,j)).toString().contains(str)){
                    ui->tvTeacher->showRow(i);
                    break;

                }
            }
        }
    }
}


void MainWindow::on_btnFindCourse_clicked()
{
    QString str = ui->editFindCourse->text();
    if(ui->editFindCourse->text().isEmpty()){
        for(int i = 0;i < m_courseModel->rowCount();++i){
            ui->tvCourse->showRow(i);
        }
    }else{
        for(int i = 0;i < m_courseModel->rowCount();++i){
            for(int j = 0;j < m_courseModel->columnCount();++j){
                ui->tvCourse->hideRow(i);
                if(m_courseModel->data(m_courseModel->index(i,j)).toString().contains(str)){
                    ui->tvCourse->showRow(i);
                    break;
                }
            }
        }
    }
}


void MainWindow::on_actSaveTeacher_triggered()
{
    QFile file(QFileDialog::getSaveFileName(0,"选择文件",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)"));

    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream stream(&file);
    stream.setCodec("UTF-8");
    for(int i = 0;i < m_teacherModel->columnCount();++i){
        stream << m_teacherModel->headerData(i,Qt::Horizontal).toString() <<"\t";
    }
    stream << "\n";

    for(int i = 0;i < m_teacherModel->rowCount();++i){
        for(int j = 0;j < m_teacherModel->columnCount();++j){
            stream << m_teacherModel->data(m_teacherModel->index(i,j)).toString() << "\t";
        }
        stream << "\n";
    }
    file.close();
    QMessageBox::information(0,"提示","保存成功!");

}


void MainWindow::on_actSaveCourse_triggered()
{
    QFile file(QFileDialog::getSaveFileName(0,"选择文件",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)"));

    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream stream(&file);
    stream.setCodec("UTF-8");
    for(int i = 0;i < m_courseModel->columnCount();++i){
        stream << m_courseModel->headerData(i,Qt::Horizontal).toString() <<"\t";
    }
    stream << "\n";

    for(int i = 0;i < m_courseModel->rowCount();++i){
        for(int j = 0;j < m_courseModel->columnCount();++j){
            stream << m_courseModel->data(m_courseModel->index(i,j)).toString() << "\t";
        }
        stream << "\n";
    }
    file.close();
    QMessageBox::information(0,"提示","保存成功!");
}

